﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace Cms.BLL
{
    /// <summary>
    /// 数据源接口
    /// </summary>
    public interface IDataSource
    {
        /// <summary>
        /// 根据数据源填充模板
        /// </summary>
        /// <param name="Template"></param>
        /// <returns></returns>
        string Builder(string Template);
    }
    /// <summary>
    /// 文本数据源
    /// </summary>
    public class TextDataSource : IDataSource
    {
        #region IDataSource 成员

        public string Builder(string Template)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
    /// <summary>
    /// RSS 数据源
    /// </summary>
    public class RssDataSource : IDataSource
    {
        static string defaultTemplate = "";
        static string defaultTemplateUrl = "App_Data\\HtmlTemplate\\RssSimple.html";
        #region IDataSource 成员
        public Model.RssBody rssBody = null;
        public RssDataSource(Model.RssBody _rssBody) {
            rssBody = _rssBody;
        }
        public string Builder(string Template)
        {
            if (String.IsNullOrEmpty(Template)) {
                if (String.IsNullOrEmpty(defaultTemplate)) {
                    string path=AppDomain.CurrentDomain.BaseDirectory;
                    path = path + defaultTemplateUrl;
                   defaultTemplate= System.IO.File.ReadAllText(path);
                }
                Template = defaultTemplate;
            }
            EntLib.StringTemplate tempate = new EntLib.StringTemplate(Template);
            tempate.Put("RssBody", rssBody);
            return tempate.ReplaceAll();
        }

        #endregion
    }
    /// <summary>
    /// Sql数据源
    /// </summary>
    public class SqlDataSource : IDataSource
    {
        static string defaultTemplate = "";
        static string defaultTemplateUrl = "App_Data\\HtmlTemplate\\SqlSimple.html";
        private DataTable dtSource = null;
        public SqlDataSource(string sql)
        {
            dtSource = BLL.PageLog.Query(sql) ;//BaseDao<string, string>.ExecuteSQL(sql);
        }
        #region IDataSource 成员

        public string Builder(string Template)
        {
            if (String.IsNullOrEmpty(Template))
            {
                if (String.IsNullOrEmpty(defaultTemplate))
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory;
                    path = path + defaultTemplateUrl;
                    defaultTemplate = System.IO.File.ReadAllText(path);
                }
                Template = defaultTemplate;
            }
            EntLib.StringTemplate tempate = new EntLib.StringTemplate(Template);
            tempate.Put("sqlModel", dtSource);
            return tempate.ReplaceAll();
            //throw new NotImplementedException();
        }

        #endregion
    }
 
 

}
